package com.baolai.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baolai.dto.RecordGoods;
import com.baolai.entity.GoodsRecord;
import com.baolai.mapper.GoodsRecordMapper;
import com.baolai.service.GoodsRecordService;
import com.baolai.tool.RandomIdTool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class GoodsRecordServiceImpl extends ServiceImpl<GoodsRecordMapper, GoodsRecord> implements GoodsRecordService {

    @Autowired
    GoodsRecordMapper goodsRecordMapper;

    @Override
    public boolean addGoodsRecord(String type, String goodsId, String site, Integer number, String account, String name) {
        GoodsRecord goodsRecord = new GoodsRecord();
        goodsRecord.setId(RandomIdTool.createRandomId());
        goodsRecord.setType(type);
        goodsRecord.setGoodsId(goodsId);
        goodsRecord.setSite(site);
        goodsRecord.setNumber(number);
        goodsRecord.setAccount(account);
        goodsRecord.setName(name);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        goodsRecord.setRecordDate(sdf.format(new Date(System.currentTimeMillis())));
        return this.save(goodsRecord);
    }

    @Override
    public JSONObject listGoodsRecord(Integer current, Integer size) {
        JSONObject jsonObject = new JSONObject();
        Integer total = this.count();
        Integer pages = (int)Math.ceil(total/size);
        jsonObject.put("total",total);
        jsonObject.put("pages",pages);
        List<RecordGoods> recordGoods = goodsRecordMapper.listRecordGoods((current-1)*size,size);
        jsonObject.put("records",recordGoods);
        return jsonObject;
    }

    @Override
    public JSONObject queryGoodsRecord(String begin, String end, Integer current, Integer size) {
        JSONObject jsonObject = new JSONObject();
        QueryWrapper<GoodsRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("record_date",begin).le("record_date",end);
        Integer total = this.count(queryWrapper);
        Integer pages = (int)Math.ceil(total/size);
        jsonObject.put("total",total);
        jsonObject.put("pages",pages);
        List<RecordGoods> recordGoods = goodsRecordMapper.queryRecordGoods(begin,end,(current-1)*size,size);
        jsonObject.put("records",recordGoods);
        return jsonObject;
    }

    @Override
    public JSONObject queryGoodsRecordByGoodsId(String goodsId, String begin, String end, Integer current, Integer size) {
        JSONObject jsonObject = new JSONObject();
        QueryWrapper<GoodsRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId).ge("record_date",begin).le("record_date",end);
        Integer total = this.count(queryWrapper);
        Integer pages = (int)Math.ceil(total/size);
        jsonObject.put("total",total);
        jsonObject.put("pages",pages);
        List<RecordGoods> recordGoods = goodsRecordMapper.queryRecordGoodsByGoodsId(goodsId,begin,end,(current-1)*size,size);
        jsonObject.put("records",recordGoods);
        return jsonObject;
    }
}
